﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;

namespace PromotionEngine
{
    public partial class Item_Property : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.ItemProperty> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.ItemProperty> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        public class ResponseDetail
        {
            public List<Model.ItemPropertyDetail> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.ItemPropertyDetail> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetItemPropertys()
        {
            List<Model.ItemProperty> lstItemProperty = new List<Model.ItemProperty>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllItemProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
            ConfigurationManager.AppSettings["PropertyValueType"].ToString(), bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ItemProperty itemProperty = new Model.ItemProperty();
                    itemProperty.CompanyCode = r["CompanyCode"].ToString();
                    itemProperty.CompanyName = r["CompanyName"].ToString();
                    itemProperty.PropertyCode = r["PropertyCode"].ToString();
                    itemProperty.PropertyName = r["PropertyName"].ToString();
                    itemProperty.PropertyID = r["PropertyID"].ToString();
                    itemProperty.CreatedUserID = r["CreatedUserID"].ToString();
                    itemProperty.PropertyValueType = r["PropertyValueType"].ToString();
                    itemProperty.ReferenceName = r["ReferenceName"].ToString();
                    lstItemProperty.Add(itemProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItemProperty };
                return new Response(lstItemProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateItemProperty(string oItemProperty, bool isInsert)
        {
            Model.ItemProperty itemProperty = JsonConvert.DeserializeObject<Model.ItemProperty>(oItemProperty);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        itemProperty.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        itemProperty.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonItemProperty( itemProperty.PropertyID, itemProperty.PropertyCode, itemProperty.PropertyName
                , itemProperty.CompanyCode, itemProperty.CreatedUserID,itemProperty.PropertyValueType,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteItemProperty(string propertyID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonItemProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetItemPropertyDetails(string propertyID)
        {
            List<Model.ItemPropertyDetail> lstItemPropertyDetail = new List<Model.ItemPropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllItemPropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ItemPropertyDetail itemPropertyDetail = new Model.ItemPropertyDetail();
                    itemPropertyDetail.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    itemPropertyDetail.PropertyDetailName = r["PropertyDetailName"].ToString();
                    itemPropertyDetail.PropertyDetailID = r["PropertyDetailID"].ToString();
                    itemPropertyDetail.PropertyID = r["PropertyID"].ToString();
                    itemPropertyDetail.CreatedUserID = r["CreatedUserID"].ToString();

                    lstItemPropertyDetail.Add(itemPropertyDetail);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItemPropertyDetail };
                return new ResponseDetail(lstItemPropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetAllItemPropertyDetails()
        {
            List<Model.ItemPropertyDetail> lstItemPropertyDetail = new List<Model.ItemPropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllItemPropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ItemPropertyDetail customerProperty = new Model.ItemPropertyDetail();
                    customerProperty.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    customerProperty.PropertyDetailName = r["PropertyDetailName"].ToString();
                    customerProperty.PropertyDetailID = r["PropertyDetailID"].ToString();
                    customerProperty.PropertyID = r["PropertyID"].ToString();
                    customerProperty.CreatedUserID = r["CreatedUserID"].ToString();

                    lstItemPropertyDetail.Add(customerProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItemPropertyDetail };
                return new ResponseDetail(lstItemPropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateItemPropertyDetail(string oItemPropertyDetail, bool isInsert)
        {
            Model.ItemPropertyDetail customerPropertyDetail = JsonConvert.DeserializeObject<Model.ItemPropertyDetail>(oItemPropertyDetail);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        customerPropertyDetail.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonItemPropertyDetail(customerPropertyDetail.PropertyID, customerPropertyDetail.PropertyDetailCode, customerPropertyDetail.PropertyDetailName
                                                    , customerPropertyDetail.PropertyDetailID, customerPropertyDetail.CreatedUserID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteItemPropertyDetail(string propertyDetailID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonItemPropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyDetailID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}